
#ifndef  RECOVERY_COMMON_H
#define  RECOVERY_COMMON_H


namespace recovery {

template<typename T = int>
class Closable {
public:
    virtual ~Closable(){}
    virtual T close() = 0;
};

class IResource : public Closable<int>{
public:
    virtual ~IResource(){}
	virtual int open() = 0;
	virtual bool isOpen() const = 0;
};

class AbstractResource : public IResource{
public:
    AbstractResource() : opened(false) {}
    ~AbstractResource() override{}
    int open() override{
        if(!opened){
            int ret = this->openOnce();
            opened = true;
            return ret;
        }
        return -1;
    }
	int close() override {
        if(opened){
            int ret = this->closeOnce();
            opened = false;
            return ret;
        }
        return -1;
    }
	bool isOpen() const override {
        return opened;
    }

protected:
    virtual int openOnce() = 0;
    virtual int closeOnce() = 0;
    void setOpened(bool opened){
        this->opened = opened;
    }

private:
    bool opened;
};

}// end of namespace recovery



#endif   /* RECOVERY_COMMON_H */
